angular.module('FlowCharge.controller', ['FlowCharge.service', 'FlowCharge.unit']); //合在一起构建的时候

angular.module('FlowCharge.controller').controller('AppController', ['$scope', '$location', function ($scope, $location) {

        $scope.getClass = function (path) {
            return ($location.path().substr(0, path.length) === path) ? 'active' : '';
        };


    }]);

//订单确认控制器
angular.module('FlowCharge.controller').controller('OrderController', ['$scope', '$http', 'PayMentService', function ($scope, $http, payMent) {

        $scope.submit = function () { //微信支付提交

        };
    }]);

//实名制控制器
angular.module('FlowCharge.controller').controller('RealNameController', ['$scope', '$http', '$vaild', 'DtoService', function ($scope, $http, $vaild, dto) {
        $scope.vo = {//提交数据到后台
            phone: "",
            idCard: "",
            file1: "", //u
            file2: "",
            file3: "",
            cardnum: [""] //方法控制
        }; //vo表单对象

        $scope.ui = { //ui控制对象

        };
        $scope.dirfun = { //指令注入方法

        };



        $scope.submit = function () {

            if ($vaild.isBlank($scope.vo.phone) || !$vaild.isMobile($scope.vo.phone)) {
                alert("手机号未填写或格式不正确");
                return;
            }
            if ($vaild.isBlank($scope.vo.idCard) || !$vaild.isCardNo($scope.vo.idCard)) {
                alert("身份证未填写或格式不正确");
                return;
            }
            if ($vaild.isBlank($scope.vo.file)) {
                alert("请上传身份证正面");
                return;
            }
            if ($vaild.isBlank($scope.vo.file1)) {
                alert("请上传身份证反面");
                return;
            }
            if ($vaild.isBlank($scope.vo.file2)) {
                alert("请上传手持身份证半身照");
                return;
            }
            $("#card-form").submit();
        };
        //回调



    }]);



//卡购买控制器
angular.module('FlowCharge.controller').controller('OrderController', ['$scope', '$http','$interval','PayMentService', function ($scope, $http,$interval,payMent) {
        $scope.QRCODE_BOX_VISABLE = false;
        $scope.SUCCESS_BOX_VISABLE = false;
         $scope.wechatPay = function () { //微信支付
          
            var promise = payMent.wechat({type:'card','channel':1}); //判断支付宝or微信
            var timer;
            promise.then(function (response) {
                $scope.wechatQcode = response.code_url;
                $scope.QRCODE_BOX_VISABLE = true;
                //开始循环
                timer = $interval(function () {
                    //console.log(response);

                    $http.get('orderStatus.php?out_trade_no=' + response.out_trade_no)
                            .success(function (data) {
                                if (data.success) {
                                    alert("支付成功");
                                    //隐藏box
                                    $interval.cancel(timer);
                                    $scope.CONFIRM_BOX_VISABLE = false;
                                    $scope.QRCODE_BOX_VISABLE = false;
                                }
                            })
                            .error(function () {

                                $interval.cancel(timer);
                            });

                }, 3000, 15);
            }, function (reason) {
                alert(reason);
            });

        };
         
}

]);


//卡购买控制器
angular.module('FlowCharge.controller').controller('CardController', ['$scope', '$http', '$vaild', function ($scope, $http, $vaild) {
        $http.get('protected/search.php?type=shopMifiCard')
                .success(function (response) {

                    $scope.cardList = response['data']['list'];
                    //console.log(  $scope.cardList);
                });
        // $scope.dirfun = { //指令注入方法
        //
        // };
        $scope.vo = {
            cardItem:{},
            cardCount: 1,
            sumPrice: 0,
            selectCardIndex: -1,
            selectPayWayIndex: -1,
            address: {
                consignee: "",
                phone: "",
                street: ""
         },
        };
        
        
             

        

        $scope.ui = {
        };


        $scope.cardCount = 1;
        $scope.sumPrice = 0;
        $scope.selectCardIndex = -1;
        $scope.selectPayWayIndex = -1;
        $scope.selectCard = function (index) {
            $scope.selectCardIndex = index;
            $scope.calSumPrice();
            $scope.vo.cardItem = $scope.cardList[$scope.selectCardIndex];
        };

        $scope.increase = function () {
            $scope.cardCount += 1;
            $scope.calSumPrice();
        };
        $scope.reduce = function () {
            if ($scope.cardCount == 1) {
                return;
            }
            ;
            $scope.cardCount -= 1;
            $scope.calSumPrice();
        };

        $scope.calSumPrice = function () {
            $scope.sumPrice = $scope.cardCount * $scope.cardList[$scope.selectCardIndex]['cost_price'];
        };

        $scope.selectPayWay = function (index) {//选择支付方式
            $scope.vo.selectPayWayIndex = index;
        };

        $scope.vaildForm = function () {
           
       if ($scope.selectCardIndex < 0) {
                alert("请选择卡种");
                return false;
            }
            if ($vaild.isBlank($scope.vo.address.street)) {
                alert("请输入地址");
                return false;
            }
            if ($vaild.isBlank($scope.vo.address.consignee)) {
                alert("请输入收件人");
                return false;
            }
            if ($vaild.isBlank($scope.vo.address.phone)) {
                alert("请输入手机号码");
                return false;
            }
            if (!$vaild.isMobile($scope.vo.address.phone)) {
                alert("手机号码格式不正确");
                return false;
            }

            if ($vaild.equal($scope.vo.address.provinceId, 0)) {
                alert("请选择省份");
                return false;
            }
            if ($vaild.equal($scope.vo.address.cityId, 0)) {
                alert("请选择城市");
                return false;
            }
            if ($vaild.equal($scope.vo.address.districtId, 0)) {
                alert("请选择区域");
                return false;
            }
            if ($vaild.equal($scope.vo.selectPayWayIndex, -1)) {
                alert("请选择支付方式");
                return false;
            }
            return true;

        };

       

        $scope.submit = function () { //检查字段 表单应用
            
            if (!this.vaildForm()) {

            } else {
             
      
                $http({"url": "protected/card_buy_save.php",
                    "method": "post",
                    
                    "data": $.param($scope.vo),
                    "headers": {'Content-Type': 'application/x-www-form-urlencoded'},
                    
                }).success(function () {


                });

            }

        };



    }
]);
//充值记录控制器
angular.module('FlowCharge.controller').controller('RecordController', ['$scope', '$http', function ($scope, $http) {
        $scope.SEARCH_BUTTON_DISABLE = false;//按钮状态禁用
        $scope.RECORD_CONTAINER_VISABLE = false;
        $scope.phone = "";
        $scope.search = function () { // $('.combo-input').val()

            $scope.phone = $scope.phone.replace(/\s/g, "");
            if ($scope.phone.length == 0) {
                alert("你输入的卡号无效");
                return;
            }

            $scope.SEARCH_BUTTON_DISABLE = true;//按钮状态禁用
            $http.get('protected/search.php?type=paycheck_record&phone=' + $scope.phone)
                    .success(function (response) {
                        $scope.SEARCH_BUTTON_DISABLE = false;
                        $scope.RECORD_CONTAINER_VISABLE = true;
                        $scope.recordList = response['data']['list'];
                    })
        }

    }
]);



//充值流量控制器
angular.module('FlowCharge.controller').controller('CzController', ['$scope', '$http', '$interval', 'HistorySerivce', 'PayMentService', function ($scope, $http, $interval, history, payMent) {

        //初始化ui变量
        $scope.SEARCH_BUTTON_DISABLE = false;
        $scope.MEAL_CONTAINER_VISABLE = false;
        $scope.MONTH_MEAL_VISABLE = false;
        $scope.YEAR_MEAL_VISABLE = false;
        $scope.CONFIRM_BOX_VISABLE = false;
        $scope.QRCODE_BOX_VISABLE = false;
        $scope.SUCCESS_BOX_VISABLE = false;


        //初始化数据
        $scope.selectMonthMealIndex = -1;
        $scope.selectYearMealIndex = -1;
        $scope.selectPayWayIndex = -1;
        $scope.month_meals = [];
        $scope.year_meals = [];
        $scope.phone = "";//手机号码



        $scope.search = function () { // $('.combo-input').val()

            $scope.phone = $scope.phone.replace(/\s/g, "");
            if ($scope.phone.length == 0) {
                alert("你输入的卡号无效");
                return;
            }

            $scope.SEARCH_BUTTON_DISABLE = true;//按钮状态禁用
            $http.get('protected/search.php?phone=' + $scope.phone)
                    .success(function (response) {
                        //console.log(response);
                        $scope.MEAL_CONTAINER_VISABLE = true;
                        $scope.SEARCH_BUTTON_DISABLE = false;
                        if (!response.success) {
                            alert("你输入的卡号无效");
                            return;
                        }
                        $scope.month_meals = response.month.result.list;
                        $scope.year_meals = response.year.result.list;
                        $scope.MONTH_MEAL_VISABLE = $scope.month_meals.length ? true : false;
                        $scope.YEAR_MEAL_VISABLE = $scope.year_meals.length ? true : false


                    });
            //加入历史记录
            history.addHistory($scope.phone);


        }
        //提交到后台
        var timer;
        $scope.submit = function () {
            $interval.cancel(timer);
            var data = {};
            data['rechargeItemId'] = $scope.selectMeal.id;
            data['phone'] = $scope.phone;//$scope.getSelectVal ();
            data['payWay'] = $scope.selectPayWayIndex;
            data['type'] = "flow";

            //console.log(payment.wechat);
            var promise = payMent.wechat(data); //判断支付宝or微信
            promise.then(function (response) {
                $scope.wechatQcode = response.code_url;
                $scope.QRCODE_BOX_VISABLE = true;
                //开始循环
                timer = $interval(function () {
                    //console.log(response);

                    $http.get('protected/query.php?out_trade_no=' + response.out_trade_no)
                            .success(function (data) {
                                if (data.success) {
                                    alert("支付成功");
                                    $scope.SUCCESS_BOX_VISABLE = true;
                                    //隐藏box
                                    $interval.cancel(timer);
                                    $scope.CONFIRM_BOX_VISABLE = false;
                                    $scope.QRCODE_BOX_VISABLE = false;
                                }
                            })
                            .error(function () {

                                $interval.cancel(timer);
                            })

                }, 3000, 15);
            }, function (reason) {
                alert(reason);
            })

        }

        //  console.log($http);
        //$scope = //ajax请求

        //$scope.month_meals = [{name:"100M",price:"1"},{name:"300M",price:"3"},{name:"500M",price:"5"},{name:"1G",price:"10"},{name:"3G",price:"30"}];
        $scope.selectMonthMeal = function (index) { //选择月份套餐
            $scope.selectYearMealIndex = -1;
            $scope.selectMonthMealIndex = index;//选择回调，价钱，描述
            $scope.selectMeal = $scope.month_meals[index];

        }
        $scope.selectYearMeal = function (index) { //选择月份套餐
            $scope.selectMonthMealIndex = -1;
            $scope.selectYearMealIndex = index;//选择回调，价钱，描述
            $scope.selectMeal = $scope.year_meals[index];

        }

        $scope.selectPayWay = function (index) {//选择支付方式
            $scope.selectPayWayIndex = index;
        }

        $scope.confirmInfo = function () { //确认提交信息

            if ($scope.selectMonthMealIndex < 0 && $scope.selectYearMealIndex < 0) {
                alert("请选择充值套餐");
                return;
            }

            if ($scope.selectPayWayIndex < 0) {
                alert("请选择充值方式");
                return;
            }
            $scope.CONFIRM_BOX_VISABLE = true;
            //支付宝&微信

        }
    }]);
